var ctx;
var cellId;
var billId;
var payWay ;
var beginTime ;
var endTime;
var total;
var bid;
var billNo;
var pSize;

function setCtx(ctx){
	this.ctx = ctx;
}


//判断是否为空
function ifNull(mess){
	if(mess == null || mess == undefined){
		mess = "";
	}
	
	return mess;
}

function checkDate () {
	beginDate = $("#beginTime").val();
	endDate = $("#endTime").val();
	//解决ie9以下placeholder与value重复的问题
	if(beginDate == $("#beginDate").attr("placeholder")){
		beginDate = "";
	}
	if(endDate == $("#endDate").attr("placeholder")){
		endDate = "";
	}
    var start=new Date(beginDate.replace("-", "/").replace("-", "/"));  
    var end = new Date(endDate.replace("-", "/").replace("-", "/"));  
    
    if(end < start){
    	layer.msg("结束时间应大于起始时间",{
			icon: 3,
			time: 1000 //2秒关闭（如果不配置，默认是3秒）
		});
    	return false;
    }
    return true;
}

//条件搜索用户列表
function getDetailList(){
	payWay = 0;
	billId = ifNull($("#billId").val());
	beginTime = ifNull($("#beginTime").val());
	endTime = ifNull($("#endTime").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(billId == $("#billId").attr("placeholder")){
		billId = "";
	}
	if (checkDate()) {
		loadContent("/feeBillDetail/detailList?pNo=1&cellId="+ cellId + "&payWay="+ payWay+ "&billId="+ 
				billId+ "&beginTime="+ beginTime+ "&endTime="+ endTime + "&pSize="+ pSize);
	}
}

//跳转到指定页面
function gotoBillPage(page) {
	if(page == null){
		page = $(".inputclass").val();
	}
	payWay = 0;
	billId = ifNull($("#billId").val());
	beginTime = ifNull($("#beginTime").val());
	endTime = ifNull($("#endTime").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(billId == $("#billId").attr("placeholder")){
		billId = "";
	}
	
	loadContent("/feeBillDetail/detailList?pNo="+page+"&cellId="+ cellId + "&payWay="+ payWay+ "&billId="+ 
			billId+ "&beginTime="+ beginTime+ "&endTime="+ endTime + "&pSize="+ pSize);
}

//跳转到上一页或者下一页
function gotoPage(page){
	payWay = 0;
	billId = ifNull($("#billId").val());
	beginTime = ifNull($("#beginTime").val());
	endTime = ifNull($("#endTime").val());
	pSize = ifNull($("#pSize").val());
	//解决ie9以下placeholder与value重复的问题
	if(billId == $("#billId").attr("placeholder")){
		billId = "";
	}
	if (checkDate()) {
		loadContent("/feeBillDetail/detailList?pNo="+page+"&cellId="+ cellId + "&payWay="+ payWay+ "&billId="+ 
				billId+ "&beginTime="+ beginTime+ "&endTime="+ endTime + "&pSize="+ pSize);
	}
}

//全选和反选
function selectAll(target){    
    if(target.checked){    
        $("#tableTbody :checkbox").attr("checked", true); 
        $(target).parents("thead").siblings("tbody").children().children().each(function(){
    	    $(this).css("background","#D3F0F1");
       });
    }else{    
        $("#tableTbody :checkbox").attr("checked", false); 
        $(target).parents("thead").siblings("tbody").children().children().each(function(){
    	    $(this).css("background","");
       });
    }    

}

//鼠标滑入一行改变背景颜色
function mouseoverOne(target){
	 $(target).children().each(function(){
		  	    $(this).css("background","#D3F0F1");
		     });
}

//鼠标滑出一行恢复原来的背景颜色
function mouseoutOne(target){
	 $(target).children().each(function(){
	  	    $(this).css("background","");
	     });
}

//选择或取消选中当前记录
function selectOne(id,target){
    if(target.checked){    
        $("#detail" + id).attr("checked", true);   
        $(target).parents("tr").children().each(function(){
    	    $(this).css("background","#D3F0F1");
       });
    }else{    
        $("#detail" + id).attr("checked", false); 
        $(target).parents("tr").children().each(function(){
    	    $(this).css("background","");
       });
    } 
}

//删除多个id对应的记录
function deleteByIds(){
	if($("img[name='detailChecked'][src*=honggou]").length == 0){
		layer.msg("请选择要删除的记录!",{
			icon: 3,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		});
	}else{
		var delIds = [];
		    $("#tableTbody img[name='detailChecked'][src*=honggou]").each(function(){ 
		    	delIds.push($(this).attr("class")); 
		    }); 
		    var vals = delIds.join(',');//转换为逗号隔开的字符串 
			layer.confirm('确认删除?', {icon: 3, title:'提示'}, function(index){
			    //do something
				$.post(ctx+"/feeBillDetail/deleteInfo",{
					iDs:vals
				},function(data){
					data = JSON.parse(data);
					if(data.success){
						layer.msg("删除成功",{
							icon: 1,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						},function(){
							loadContent("/feeBillDetail/detailList");
						});
					} else {
						layer.msg(data.msg,{
							icon: 2,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						});
					}
				});
			    layer.close(index);
			});

		
	}
}

//保存备注信息
var index;

//实现字段较长时的隐藏于显示
function mouseOverInfo(item , answer){
	layer.tips(answer, '#answer' + item, {
		  tips: [3, '#2F2F2F'],
		  time: 5000
		});
}

//查看缴费信息详情
function gotoDetail (id,houseId) {
	loadContent("/feeBillDetail/gotoDetail?id="+id+"&houseId="+houseId+"&type=2");
}

function cancel (type,id) {
	if (type ==1) {
		loadContent("/feeBill/gotoDetail?houseId="+id);
	} else if (type == 2) {
		loadContent("/feeBillDetail/detailList");
	} else {
		loadContent("/feeBillDetail/delList");
	}
}

//导出缴费记录账单
function exportExcel() {
	cellId = ifNull($("#cellId").val());
	beginTime = ifNull($("#beginTime").val());
	endTime = ifNull($("#endTime").val());
	billId = ifNull($("#billId").val());
	total = ifNull($("#total").val());
	if(billId == $("#billId").attr("placeholder")){
		billId = "";
	}
	if (checkDate()) {
		if(total >0) {
			location.href=ctx +"/feeBillDetail/exportFeeBillExcel?cellId="+ cellId + "&billId="+ billId+ "&beginTime="+ 
			beginTime+ "&endTime="+ endTime;
		} else {
			layer.msg("未获取到数据，无法导出...",{
				icon: 2,
				time: 2000 //2秒关闭（如果不配置，默认是3秒）
			});
		}
	}
}
//导出发票
function exportExcelBill() {
	cellId = ifNull($("#cellId").val());
	beginTime = ifNull($("#beginTime").val());
	endTime = ifNull($("#endTime").val());
	total = ifNull($("#total").val());
	
	if(billId == $("#billId").attr("placeholder")){
		billId = "";
	}
	if (beginTime != "" && endTime != "") {
		if(total >0) {
			location.href=ctx +"/feeBillDetail/exportBillExcel?cellId="+ cellId + "&beginTime="+ 
			beginTime+ "&endTime="+ endTime;
		} else {
			layer.msg("未获取到数据，无法导出...",{
				icon: 2,
				time: 2000 //2秒关闭（如果不配置，默认是3秒）
			});
		}
	}else {
		layer.msg("请选择要导出的日期范围",{
			icon: 2,
			time: 2000 //2秒关闭（如果不配置，默认是3秒）
		},function(){
			parent.layer.close(index);
		});
	}
}
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
//添加发票
function addBillType (bid) {
	msg = "添加发票";
	url = ctx + "/feeBillDetail/addBillType?&id="+bid;
	saveOrUpdateBillType(msg, url);
	
}

//弹出添加发票框
function saveOrUpdateBillType(msg,url){
	  //官网欢迎页
	   index = layer.open({
	      type: 2,
	      //skin: 'layui-layer-lan',
	      title: msg,
	      fix: false,
	      shadeClose: true,
	      scrollbar: false,
	      maxmin: false,
	      edit: true,
	      closeBtn: 1,
	      area: ['500px', '220px'],
	      content: url,
	      btn: ['设置', '取消']
		    ,yes: function(index, layero){ //或者使用btn1
		        //按钮【按钮一】的回调
		    	var iframeWin = window[layero.find('iframe')[0]['name']]; 
		    	iframeWin.saveBillType();

		    },cancel: function(index){ //或者使用btn2
		        //按钮【按钮二】的回调
		    }
		    });
	}

function checkBillTitle() {
	var billTitle = $("#billTitle").val();
	//解决ie9以下placeholder与value重复的问题
	if(billTitle == $("#billTitle").attr("placeholder")){
		billTitle = "";
	}
	if(billTitle == null || billTitle == ""){
		layer.tips('发票抬头不能为空!', '#billTitle', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}
function checkBillNo() {
	var billNo = $("#billNo").val();
	//解决ie9以下placeholder与value重复的问题
	if(billNo == $("#billNo").attr("placeholder")){
		billNo = "";
	}
	if(billNo == null || billNo == ""){
		layer.tips('发票票号不能为空!', '#billNo', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//保存发票
function saveBillType () {
	billType = ifNull($("#billType").val());
	billTitle = ifNull($("#billTitle").val());
	billNo = ifNull($("#billNo").val());
    bid = $('#bid').val();
    if(checkBillTitle() && checkBillNo()){
	$.post(ctx+"/feeBillDetail/saveBillType",{
		id:bid,
    	billType:billType,
    	billTitle:billTitle,
    	billNo:billNo
	},function(data){
		data = JSON.parse(data);
		if(data.success){
			layer.msg("添加成功",{
				icon: 1,
				time: 2000 //2秒关闭（如果不配置，默认是3秒）
			},function(){
				parent.loadContent("/feeBillDetail/detailList");
				parent.layer.close(index);
			});
		} else {
			layer.msg(data.msg,{
				icon: 2,
				time: 2000 //2秒关闭（如果不配置，默认是3秒）
			});
		}
	});
    }
}
